<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .box {
      position: relative;
      top: 10px;
      left: 10px;
      height: 200px;
      width: 200px;
      background-color: aquamarine;
    }
  </style>
</head>

<body>
  <div class="box"></div>
  <button>预估预估一个</button>
</body>
<script>
  var box = document.getElementsByClassName('box')[0]
  var top1 = []
  var left1 = []
  box.onmousedown = function (e) {

    //当前点在盒子上的坐标
    var y = e.pageY - box.offsetTop
    var x = e.pageX - box.offsetLeft
    console.log(x, y);
    //获取移动坐标
    window.onmousemove = function (e1) {
      var t = e1.pageY - y
      var l = e1.pageX - x
      box.style.top = t + 'px'
      box.style.left = l + 'px'
      //把坐标追加到第一位
      top1.unshift(t)
      left1.unshift(l)
    }
    box.onmouseup = function (e2) {
      //鼠标松开时候，去除鼠标移动事件
      window.onmousemove = null
    }

  }
  var button = document.getElementsByTagName('button')[0]
  button.onclick = function (e) {
    var i = 0
    var interId = setInterval(
      function () {
        box.style.top = top1[i] + 'px'
        box.style.left = left1[i] + 'px'
        i++
        //如果i>=最大索引要清除定时器
        if (i >= top1.length) {
          clearInterval(interId)
          box.style.top = '10px'
          box.style.tleft = '10px'
          top1 = []
          left1 = []
        }
      }, 20)
  }
</script>

</html>